import { AxiosInstance, AxiosRequestConfig } from './types'
import Axios from './core/Axios'
import { extend } from './helpers/util'
import defaultConfig from './default'

function createInstance(defaultConfig: AxiosRequestConfig): AxiosInstance {
  const context = new Axios(defaultConfig)
  const instance = Axios.prototype.request.bind(context)

  // 通过extend方法使instance既可以作为函数被调用，也可以通过其上面的属性(get、post等)调用方法
  extend(instance, context)
  return instance as AxiosInstance
}

const axios = createInstance(defaultConfig)

export default axios
